package com.edu.compile.template;

import com.edu.compile.ParserHolder;
import com.edu.compile.template.buffer.Buffer;
import com.edu.model.tree.v2.TreeNode;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 抽象节点解析，parse方法模板方法，限定了几个方法的调用顺序
 *
 * @author yanzhao
 * @date 2021/10/21 00:05
 * @since 1.0.0
 */
@Data
public abstract class AbstractNodeTemplateParse implements NodeTemplateParse {

    public AbstractNodeTemplateParse(String nodeId, String nodeType, String nodeName, String parentNodeId) {
        this.nodeId = nodeId;
        this.nodeType = nodeType;
        this.nodeName = nodeName;
        this.parentNodeId = parentNodeId;
    }

    private boolean isLoop = false;

    private String parentNodeId;

    private String eventId;

    private String nodeId;

    private String nodeType;

    private String nodeName;

    public boolean isLoop() {
        return isLoop;
    }

    public void setLoop(boolean loop) {
        isLoop = loop;
    }

    private Map<String, String> multipleInputs = new HashMap<>();

    private boolean asyncOfPromise = false;

    public Map<String, String> getMultipleInputs() {
        return multipleInputs;
    }

    public void putInput(String key, String input) {
        this.multipleInputs.put(key, input);
    }

    public boolean isAsyncOfPromise() {
        return asyncOfPromise;
    }

    public void setAsyncOfPromise(boolean asyncOfPromise) {
        this.asyncOfPromise = asyncOfPromise;
    }

    public final void parse(TemplatePolymerization handler, TreeNode<NodeTemplateParse> treeNode, Buffer buffer) {
        parseMethodCall(handler, treeNode, buffer);
        postParseMethodBody(handler, buffer);
    }

    @Override
    public void postParseMethodBody(TemplatePolymerization handler, Buffer buffer) {
        handler.addMethod(ParserHolder.templateParser.parse(this));
    }

    @Override
    public abstract String parseMethodCall(TemplatePolymerization handler, TreeNode<NodeTemplateParse> treeNode, Buffer buffer);

}

